//
//  UIView+setup.m
//  InitDemo
//
//  Created by 江伟 on 2018/8/15.
//  Copyright © 2018年 weiJiang. All rights reserved.
//

#import "UIView+setup.h"
#import <objc/runtime.h>
@implementation UIView_setup

@end


@implementation UILabel(setup)
- (instancetype)initWithBackgroundColor:(UIColor *)backColor text:(NSString *)text textColor:(UIColor *)textColor font:(UIFont *)font textAlignment:(NSTextAlignment)alignment {
    if (self = [super init]) {
        self.backgroundColor = backColor;
        self.textAlignment = alignment;
        self.font = font;
        self.textColor = textColor;
        self.text = text;
    }
    return self;
}
@end

@implementation UIButton (setup)
static NSString *keyOfUseCategoryMethod;//用分类方法创建的button，关联对象的key
static NSString *keyOfBlock;

+ (UIButton *)setupBtnWithBackgroundColor:(UIColor *)bkColor title:(NSString *)title titleColor:(UIColor *)tColor isBkImg:(BOOL)isBkImg image:(UIImage *)img target:(id)target selector:(SEL)selector {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.backgroundColor = bkColor;
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:tColor forState:UIControlStateNormal];
    if (isBkImg) {
        [btn setBackgroundImage:img forState:UIControlStateNormal];
    } else {
        [btn setImage:img forState:UIControlStateNormal];
    }
    [btn addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    [btn addTarget:btn action:@selector(clickBtn) forControlEvents:UIControlEventTouchUpInside];
    return btn;
}

- (void)clickBtn {
    if (self.clickBlock) {
        self.clickBlock(self);
    }
}

- (void)setClickBlock:(ClickBtnBlock)clickBlock {
    objc_setAssociatedObject(self, &keyOfBlock, clickBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (ClickBtnBlock)clickBlock {
    return objc_getAssociatedObject(self, &keyOfBlock);
}

@end
